package com.gdk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.gdk.service.ArticleService;
import com.gdk.service.CategoryService;
import com.gdk.service.FairService;
import com.gdk.service.GoodsService;
import com.gdk.service.HXService;
import com.gdk.service.HotelService;
import com.gdk.service.LoginService;
import com.gdk.service.QyService;
import com.gdk.service.ShopService;
import com.gdk.service.UserService;
import com.gdk.utils.HUtil;
import com.gdk.utils.MapTypeAdapter;
import com.gdk.utils.XVDateUtils;


/**
 *用户controller
 * */

@Controller
public class UserController {
	
	@Autowired
    private UserService userService;
	@Autowired
    private QyService qyService;
	@Autowired
    private ShopService shopService;
	@Autowired
    private HXService hxService;
	@Autowired
    private HotelService hotelService;
	@Autowired
    private FairService fairService;
	/**
	 * 查询所有用户
	 * */
	@RequestMapping("/selectAllUser.do")
	public String selectAllUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allUser = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		allUser = userService.selectAllUser(map);
		
/*
		double z_yj = 0.00;
		double z_jf = 0.00;
		
		for (Map<String, Object> map2 : allUser) {
			double u_integral = Double.valueOf(map2.get("u_integral").toString());
			double u_commission = Double.valueOf(map2.get("u_commission").toString());
			z_yj = z_yj + u_commission;
			z_jf = z_jf + u_integral;
		}*/
		
		int itemCount = allUser.size();
		
		map.put("allUserItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/user/user-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allUser = userService.selectAllUser(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allUser) {
			if(map2.get("addtime") != null) {
				int p = Integer.valueOf(map2.get("addtime").toString());
				if(p != 0) {
					map2.put("addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			//查询出所有会议
			List<Map<String, Object>> allFair = fairService.selectUserBindFair(map2);
			
			request.setAttribute("allFair",allFair);
			map2.put("allFair", allFair);
			
		}
	/*	request.setAttribute("z_yj",z_yj);
		request.setAttribute("z_jf",z_jf);*/
		request.setAttribute("allUser",allUser);
		
	
		
		return "jsp/user/user-list";
	}
	
	/**
	 *修改前查询用户详情
	 * */
	@RequestMapping("/preSelectUserDetails.do")
	public String preSelectUserDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> userDetails = userService.selectUserDetails(map);
		
		request.setAttribute("userDetails",userDetails);
	
		//int u_sex = Integer.valueOf(userDetails.get("u_sex").toString());
		//int u_type = Integer.valueOf(userDetails.get("u_type").toString());
		//int hr_status = 1;
		
		//查询出所有空房间根据用户身份和类型
	/*	List<Map<String, Object>> allBedBySexAndTypeAndStatus = hotelService.selectAllBedBySexAndTypeAndStatus(HUtil.putInMap("hr_sex",u_sex,"hr_type",u_type,"hr_status",hr_status));
		
		
		request.setAttribute("allBedBySexAndTypeAndStatus",allBedBySexAndTypeAndStatus);
		*/
		//查询出当前住房
		List<Map<String, Object>> userHotel = hotelService.selectUserBindHotelDetails(map);
		request.setAttribute("userHotel",userHotel);
		
		//查询出所有空房
		List<Map<String, Object>> allHotelByStatus = hotelService.selectAllHotelByStatus(map);
		//request.setAttribute("allHotelByStatus",allHotelByStatus);
		
		
		//查询出已选会议
		List<Map<String, Object>> userBindFair = fairService.selectUserBindFair(map);
		request.setAttribute("userBindFair",userBindFair);
		String userBindFairStr = "";
		for (int i = 0; i < userBindFair.size(); i++) {
			if(i != userBindFair.size()-1) {
				userBindFairStr = userBindFairStr + userBindFair.get(i).get("f_id").toString()+",";
			}else {
				userBindFairStr = userBindFairStr + userBindFair.get(i).get("f_id").toString();
			}
			
			
			
			
		}
		request.setAttribute("userBindFairStr",userBindFairStr);
		
		//查询关联会议关联的空床位
		allHotelByStatus = hotelService.selectAllBindFairAndHotelFreeBed(HUtil.putInMap("f_id",userBindFairStr));		
		request.setAttribute("allHotelByStatus",allHotelByStatus);
		
		Gson gson = new GsonBuilder()
                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {
                }.getType(), new MapTypeAdapter()).create();
		String userBindFairJson = gson.toJson(userBindFair);
		
		request.setAttribute("userBindFairJson",userBindFairJson);
		
		return "jsp/user/user-edit";
	}
	
	/**
	 * 修改用户
	 * */
	@ResponseBody
	@RequestMapping("/updateUser.do")
	public int updateUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("用户 : "+map);
		//手机号码比对
		List<Map<String, Object>> preUserDetails = userService.selectUserDetailsByPhone(HUtil.putInMap("u_phone",map.get("u_phone")));
		if(preUserDetails.size() > 0) {
			for (Map<String, Object> map2 : preUserDetails) {
				int p_u_id = Integer.valueOf(map2.get("u_id").toString());
				int u_id = Integer.valueOf(map.get("u_id").toString());				
				System.out.println("p_u_id :"+p_u_id);
				System.out.println("u_id :"+u_id);
				if(p_u_id != u_id) {
					//手机号码已存在
					return 2;
				}
				
			}
			
		}
		int shop = userService.updateUserDetails(map);
		if(shop == 1) {
			//绑定用户和会议座次
			System.out.println("tableData: "+map.get("tableData"));
			//所选会议及座次
			//List<Map<String, Object>> tableData = new ArrayList<Map<String, Object>>();
			//String[] tableData = map.get("tableData").toString().split(",");
			
			Gson gson = new GsonBuilder()
	                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {
	                }.getType(), new MapTypeAdapter()).create();
			
			List<Map<String, Object>> tableData =
				       gson.fromJson(map.get("tableData").toString(), new TypeToken<List<Map<String, Object>>>() {
				       }.getType());
			
			List<Map<String, Object>> userBindFair = fairService.selectUserBindFair(map);
			
			Set<Integer> n_ds = new HashSet();
			
			for (Map<String, Object> map2 : tableData) {
				n_ds.add(Integer.valueOf(map2.get("f_id").toString()));
				System.out.println("所选会议 id : "+map2.get("f_id"));
				System.out.println("所选会议 zuoci  : "+map2.get("si_zc"));
				
				//按用户id和会议id查询
				//已存在的比对座次是否修改
				//不存在新增
				//数据库存在 本次数据不存在  删除数据库中多余的数据
				
				int f_id = Integer.valueOf(map2.get("f_id").toString()); 
				String si_zc = map2.get("si_zc").toString();
				int flag = 0;
				for (Map<String, Object> map3 : userBindFair) {
					int y_f_id = Integer.valueOf(map3.get("f_id").toString()); 
					String y_si_zc = map3.get("si_zc").toString();
					if(f_id == y_f_id) {
						//比对座次
						if(!si_zc.equals(y_si_zc)) {
							//不一致,修改数据库座次
							fairService.updateBindFairAndUser(HUtil.putInMap("si_zc",si_zc,"si_id",map3.get("si_id")));
						}
						flag = 1;
					}
				}
				
				if(flag == 0) {
					//绑定用户和会议
					int u_id = Integer.valueOf(map.get("u_id").toString());
					Map<String,Object> bindMap = new HashMap<String,Object>();
					bindMap.put("u_id", u_id);
					bindMap.put("f_id", map2.get("f_id"));
					bindMap.put("si_zc", map2.get("si_zc"));
					fairService.bindUserAndFair(bindMap);
					flag = 0;
				}
			}
			
			//数据库存在 本次数据不存在  删除数据库中多余的数据
			//需要重新查询最新数据库数据
			List<Map<String, Object>> newUserBindFair = fairService.selectUserBindFair(map);
			
			Set<Integer> yds = new HashSet();
			for (Map<String, Object> map4 : newUserBindFair) {
				yds.add(Integer.valueOf(map4.get("f_id").toString()));
			}
			
			Set<Integer> cy = new HashSet();
			
			for (int y : yds) {
				int fff = 0;
				for (int n : n_ds) {
					if(y == n) {
						fff = 1;
					}
				}
				if(fff == 0) {
					cy.add(y);
				}
			}
			
			
			for (int ds_f_id : cy) {
				System.out.println("待删会议id : "+ds_f_id); 
				
			}
			for (int ds_f_id : cy) {
				fairService.deleteBindFairAndUser(HUtil.putInMap("f_id",ds_f_id,"u_id",map.get("u_id")));
			}
			
		/*	
		
			if(1==1) {
				return 0;
			}
			
			*/
			
			
			if(map.get("hs_hr_id") != null && !"".equals(map.get("hs_hr_id").toString())) {
				System.out.println("选择了新的房间");
				
				//是否选择了新的房间
				int hs_hr_id = Integer.valueOf(map.get("hs_hr_id").toString());
				//选择了新房间,并且查询新房间的状态
				Map<String, Object> hotelRoomDetails = hotelService.selectHotelRoomDetails(HUtil.putInMap("hr_id",hs_hr_id));
				
				
				//查询旧房间的详情
				if(map.get("hh_dq") !=null && !"".equals(map.get("hh_dq").toString())) {
					
					System.out.println("存在旧的绑定  :  "+map.get("hh_dq").toString());
					
					int j_hr_id = Integer.valueOf(map.get("hh_dq").toString());
					Map<String, Object> oldHotelRoomDetails = hotelService.selectHotelRoomDetails(HUtil.putInMap("hr_id",j_hr_id));
					//查询旧的绑定详情
					List<Map<String, Object>> userBindHotel = hotelService.selectUserBindHotel(HUtil.putInMap("u_id",map.get("u_id")));
					System.out.println("查询出旧的绑定信息");
					for (Map<String, Object> map2 : userBindHotel) {
						//修改旧的床位状态
						hotelService.updateHotelBedDetails(HUtil.putInMap("hb_id",map2.get("hb_id"),"hb_status",0,"hb_info","后台已解绑 :"+XVDateUtils.MillisFormat(XVDateUtils.getCurrenTimeMillis())));
						//修改旧房间的入驻状态 
						int rz_status = Integer.valueOf(oldHotelRoomDetails.get("hr_status").toString());
						if(rz_status > 0) {
							hotelService.updateHotelRoomDetails(HUtil.putInMap("hr_id",j_hr_id,"hr_status",rz_status-1,"hrn_data_info","后台已解绑"+XVDateUtils.MillisFormat(XVDateUtils.getCurrenTimeMillis())));
						}
					}
				}
				//删除旧的绑定(根据用户)
				hotelService.delBind(HUtil.putInMap("u_id",map.get("u_id")));
				
				//查询出空闲床位后绑定新房间床位,修改新房间入驻状态
				//查询指定房间的空闲床位
				
				List<Map<String, Object>> allHotelBedByRoom = hotelService.selectAllHotelBedByRoom(HUtil.putInMap("hr_id",hs_hr_id));
				for (Map<String, Object> map2 : allHotelBedByRoom) {
					int x_hb_status = Integer.valueOf(map2.get("hb_status").toString());
					if(x_hb_status == 0) {
						//分配房间床位
						int hb_id = Integer.valueOf(map2.get("hb_id").toString());
						
						//修改床位状态
						hotelService.updateHotelBedDetails(HUtil.putInMap("hb_id",hb_id,"hb_status",1,"hb_info","后台分配 :"+XVDateUtils.MillisFormat(XVDateUtils.getCurrenTimeMillis())));
						//修改新的房间入驻状态
						int x_rz_status = Integer.valueOf(hotelRoomDetails.get("hr_status").toString());
						if(x_rz_status == 0) {
							
							Map<String, Object> userDetails = userService.selectUserDetails(map);
							
							hotelService.updateHotelRoomDetails(HUtil.putInMap("hr_id",hs_hr_id,"hr_type",userDetails.get("u_type"),"hr_sex",userDetails.get("u_sex")));
						}
						if(x_rz_status <2 ) {
							hotelService.updateHotelRoomDetails(HUtil.putInMap("hr_id",hs_hr_id,"hr_status",x_rz_status+1,"hrn_data_info","后台分配"+XVDateUtils.MillisFormat(XVDateUtils.getCurrenTimeMillis())));
						}
						
						//删除旧的绑定关系(根据床位)
						hotelService.delBindByBed(HUtil.putInMap("hb_id",hb_id));
						//添加绑定
						Map<String,Object> bedMap = new HashMap<String,Object>();
						bedMap.put("hb_id", hb_id);
						bedMap.put("hr_id", hs_hr_id);
						bedMap.put("u_id", map.get("u_id"));
						bedMap.put("buh_addtime", XVDateUtils.getCurrenTimeMillis());
						
						hotelService.bindHotelRoomBed(bedMap);
						
						Map<String,Object> user_update = new HashMap<String,Object>();
						user_update.put("u_id", map.get("u_id"));
						user_update.put("u_rz",0);
						user_update.put("u_home",0);
						
						Map<String, Object> hotelDetails = hotelService.selectHotelDetails(HUtil.putInMap("hrn_id",hotelRoomDetails.get("hrn_id")));
						//吃饭地点
						user_update.put("u_eat_address",hotelDetails.get("hrn_eat_address"));
						//酒店
						user_update.put("u_hotel",hotelDetails.get("hrn_name"));
						//标准
						user_update.put("u_hoteltapy",hotelDetails.get("hrn_type"));
						//房间号
						user_update.put("u_hotelnumber",hotelRoomDetails.get("hr_room"));
						//床位号
						user_update.put("u_hotelnumbertel",map2.get("hb_bed"));
						//修改用户入驻状态
						userService.updateUserDetails(user_update);
						break;   //正常跳出循环
					}
				}
				
				
			}
		}
		return shop;
	}


	
	/**
	 * 查询指定用户的佣金记录
	 * */
	@RequestMapping("/selectCommissionByUser.do")
	public String selectCommissionByUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allCommissionByUser = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = userService.selectCommissionByUser(map).size();
		
		map.put("allCommissionItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/commission/commission-user-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allCommissionByUser = userService.selectCommissionByUser(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allCommissionByUser) {
			if(map2.get("c_time") != null) {
				int p = Integer.valueOf(map2.get("c_time").toString());
				if(p != 0) {
					map2.put("c_time", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		
		request.setAttribute("allCommissionByUser",allCommissionByUser);
		return "jsp/commission/commission-user-list";
	}
	
	
	/**
	 * 查询指定用户的积分记录
	 * */
	@RequestMapping("/selectIntegralByUser.do")
	public String selectIntegralByUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allIntegralByUser = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = userService.selectIntegralByUser(map).size();
		
		map.put("allIntegralItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/user/integral-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allIntegralByUser = userService.selectIntegralByUser(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allIntegralByUser) {
			if(map2.get("ir_time") != null) {
				int p = Integer.valueOf(map2.get("ir_time").toString());
				if(p != 0) {
					map2.put("ir_time", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		
		request.setAttribute("allIntegralByUser",allIntegralByUser);
		return "jsp/user/integral-list";
	}
	
	/**
	 * 查询所有认养用户
	 * */
	@RequestMapping("/selectAllUserByRy.do")
	public String selectAllUserByRy(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allUserByRy = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = userService.selectAllUserByRy(map).size();
		
		map.put("allUserByRyItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/user/user-ry-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allUserByRy = userService.selectAllUserByRy(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allUserByRy) {
			if(map2.get("u_ctime") != null) {
				int p = Integer.valueOf(map2.get("u_ctime").toString());
				if(p != 0) {
					map2.put("u_ctime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("or_c_time") != null) {
				int p = Integer.valueOf(map2.get("or_c_time").toString());
				if(p != 0) {
					map2.put("or_c_time", XVDateUtils.MillisFormat(p));	
				}
			}
		}
		
		
		request.setAttribute("allUserByRy",allUserByRy);
		return "jsp/user/user-ry-list";
	}
	
	/**
	 * 用户赠送权益前查询
	 * */

	@RequestMapping("/preBindQyByUser.do")
	public String preBindQyByOrder(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
	
		
		 List<Map<String, Object>> allQy = qyService.selectAllQy();
		 request.setAttribute("allQy", allQy);
		 
		 request.setAttribute("map", map);
		return "jsp/user/user-qy-bind";
	}
	
	/**
	 * 赠送权益
	 * */
	@ResponseBody
	@RequestMapping("/bindQyByUser.do")
	public int bindQyByUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("打印看看  : "+map);
		
		//遍历fa_id
		String all_fa_id = map.get("fa_id").toString();
		
		Map<String,Object> qyMap = new HashMap<String,Object>();
		qyMap.put("u_id", map.get("u_id"));
		qyMap.put("o_id",0);
		
		int qy = 0;
		
		if(all_fa_id.contains(",")) {
			String[] split = all_fa_id.split(",");
			for (String str : split) {
				if(!str.equals("")) {
					int fa_id = Integer.valueOf(str);
					qyMap.put("fa_id", fa_id);
					//查询fa_id的详情
					Map<String, Object> qyDetails = qyService.selectQyDetails(qyMap);
					
					qyMap.put("qy_sumcount", qyDetails.get("fa_count"));
					qyMap.put("qy_count", qyDetails.get("fa_count"));
					qyMap.put("qy_type", 0);
					qyMap.put("qy_time",XVDateUtils.getCurrenTimeMillis());
					//绑定
					qy = qyService.bindQy(qyMap);
					
				}
			}
			
		}else {
			//查询fa_id的详情
			qyMap.put("fa_id", map.get("fa_id"));
			//查询fa_id的详情
			Map<String, Object> qyDetails = qyService.selectQyDetails(qyMap);
			
			qyMap.put("qy_sumcount", qyDetails.get("fa_count"));
			qyMap.put("qy_count", qyDetails.get("fa_count"));
			qyMap.put("qy_type", 0);
			qyMap.put("qy_time",XVDateUtils.getCurrenTimeMillis());
			//绑定
			qy = qyService.bindQy(qyMap);
			
		}
		//int bindQy = qyService.bindQy(map);
		return qy;
	}
	
	/**
	 * 查询所有申请提现
	 * */
	@RequestMapping("/selectAllApplyTX.do")
	public String selectAllApplyTX(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allApply = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		allApply = userService.selectAllApplyTX(map);
		

		
		
		int itemCount = allApply.size();
		
		map.put("allApplyItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/money/money-tx-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allApply = userService.selectAllApplyTX(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allApply) {
			if(map2.get("uo_time") != null) {
				int p = Integer.valueOf(map2.get("uo_time").toString());
				if(p != 0) {
					map2.put("uo_time", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
	
		request.setAttribute("allApply",allApply);
		return "jsp/money/money-tx-list";
	}
	
	/**
	 * 代售通过申请提现
	 * */
	@ResponseBody
	@RequestMapping("/agreeApply.do")
	public int agreeApply(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		map.put("uo_type", 1);
		System.out.println("代售提现Map : "+map);
		
		int i = userService.updateUserOrderMoneyDetails(map);
		return i;
	}
	/**
	 * 查询所有佣金申请提现
	 * */
	@RequestMapping("/selectAllUserCommissionApplyTX.do")
	public String selectAllUserCommissionApplyTX(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allApply = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		allApply = userService.selectAllUserCommissionApplyTX(map);
		

		
		
		int itemCount = allApply.size();
		
		map.put("allApplyItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/money/commission-tx-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allApply = userService.selectAllUserCommissionApplyTX(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allApply) {
			if(map2.get("c_time") != null) {
				int p = Integer.valueOf(map2.get("c_time").toString());
				if(p != 0) {
					map2.put("c_time", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
	
		request.setAttribute("allApply",allApply);
		return "jsp/money/commission-tx-list";
	}
	/**
	 * 佣金通过申请提现
	 * */
	@ResponseBody
	@RequestMapping("/agreeApplyCommission.do")
	public int agreeApplyCommission(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		map.put("c_status", 1);
		System.out.println("佣金提现Map : "+map);
		
		int i = userService.updateUserCommissionDetails(map);
		return i;
	}
	
	
	/**
	 * 用户赠送权益前查询
	 * */

	@RequestMapping("/preBindShopAndUser.do")
	public String preBindShopAndUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
	
		List<Map<String, Object>> allShop = shopService.selectAllShop1();
		
		request.setAttribute("allShop", allShop);
		
		
		Map<String, Object> userDetails = userService.selectUserDetails(map);
		
		request.setAttribute("userDetails", userDetails);
		
		return "jsp/user/user-shop-bind";
	}
	
	/**
	 * 赠送权益
	 * */
	@ResponseBody
	@RequestMapping("/bindShopAndUser.do")
	public int bindShopAndUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("打印看看  : "+map);
		
		int userDetails = userService.updateUserDetails(map);
	
		return userDetails;
	}
	
	/**
	 * 新增用户前
	 * */
	@RequestMapping("/preAddUser.do")
	public String preAddUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("打印看看  : "+map);
		
		
		
		
		
		return "jsp/user/user-add";
	}
	
	
	/**
	 * 赠送权益
	 * */
	@ResponseBody
	@RequestMapping("/addUser.do")
	public int addUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("map : "+map);
		List<Map<String, Object>> userlist = userService.selectUserDetailsByPhone(map);
		if(userlist.size() > 0) {
			//手机号码已存在
			return 2;
		}
		map.put("addtime", XVDateUtils.getCurrenTimeMillis());
		
		int userDetails = userService.addUser(map);
		if(userDetails == 1) {
			System.out.println("tableData: "+map.get("tableData"));
			//所选会议及座次
			//List<Map<String, Object>> tableData = new ArrayList<Map<String, Object>>();
			//String[] tableData = map.get("tableData").toString().split(",");
			
			Gson gson = new GsonBuilder()
	                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {
	                }.getType(), new MapTypeAdapter()).create();
			
			List<Map<String, Object>> tableData =
				       gson.fromJson(map.get("tableData").toString(), new TypeToken<List<Map<String, Object>>>() {
				       }.getType());
			
			for (Map<String, Object> map2 : tableData) {
				
				System.out.println("所选会议 id : "+map2.get("f_id"));
				System.out.println("所选会议 zuoci  : "+map2.get("si_zc"));
				//绑定用户和会议
				int u_id = Integer.valueOf(map.get("u_id").toString());
				Map<String,Object> bindMap = new HashMap<String,Object>();
				bindMap.put("u_id", u_id);
				bindMap.put("f_id", map2.get("f_id"));
				bindMap.put("si_zc", map2.get("si_zc"));
				fairService.bindUserAndFair(bindMap);
			}
		}
		
		return userDetails;
	}
	
	
	
	
	/**
	 * 统计
	 * */
	@ResponseBody
	@RequestMapping("/selectUserCount.do")
	public Map<String,Object> selectUserCount(@RequestParam Map<String,Object> map, HttpServletRequest request) {

		Map<String,Object> resultMap = new HashMap<String,Object>();
	
		
		//查询所有用户数
		List<Map<String, Object>> allUser = userService.selectAllUser(map);
		resultMap.put("user_count", allUser.size());
		//查询所有签到数
		List<Map<String, Object>> allHXLog = hxService.findAllHXLog(HUtil.putInMap("sign_in_status",1));
		resultMap.put("yhx_count", allHXLog.size());
		
	
		return resultMap;
	}
	
	
	
	/**
	 * 修改床位前查询
	 * */
	@RequestMapping("/preUpdateBindUserAndHotel.do")
	public String preUpdateBindUserAndHotel(@RequestParam Map<String,Object> map, HttpServletRequest request) {
	
		//查询用户信息
		Map<String, Object> userDetails = userService.selectUserDetails(map);
		
		//1男  2女
		int u_sex = Integer.valueOf(userDetails.get("u_sex").toString());
		// 0 销售  1学员
		int t_id = Integer.valueOf(userDetails.get("t_id").toString());
		
		List<Map<String, Object>> allHotelBed = hotelService.selectAllHotelBed(map);
		
		
		//未分配的床位
		
		//销售男
		//销售女
		//学员男
		//学员女
		
		
		for (Map<String, Object> map2 : allHotelBed) {
			//查询所有未分配的房间
		}
		
		
		return "jsp/user/user-hotel-bind";
	}
	
	
	
	
	/**
	 * 删除用户
	 * */
	@ResponseBody
	@RequestMapping("/del_user.do")
	public int del_user(@RequestParam Map<String,Object> map, HttpServletRequest request) {
	
	
		//查询用户信息
		//查询用户信息
		Map<String, Object> userDetails = userService.selectUserDetails(map);
		
		//查询分配的房间
		List<Map<String, Object>> userBindHotelList = hotelService.selectUserBindHotel(map);
		
		if(userBindHotelList.size() > 0) {
			//查询绑定的房间 
			//清除绑定信息
			//修改房间信息
			for (Map<String, Object> map2 : userBindHotelList) {
				int hb_id = Integer.valueOf(map2.get("hb_id").toString());
				int hr_id = Integer.valueOf(map2.get("hr_id").toString());
			
				int bedStatus = hotelService.updateHotelBedDetails(HUtil.putInMap("hb_id",hb_id,"hb_status",0));			
				
				//查询指定房间详情
				Map<String, Object> hotelRoomDetails = hotelService.selectHotelRoomDetails(HUtil.putInMap("hr_id",hr_id));
				
				int hr_status = Integer.valueOf(hotelRoomDetails.get("hr_status").toString());
				
				if(hr_status > 0 ) {
					hotelService.updateHotelRoomDetails(HUtil.putInMap("hr_id",hr_id,"hr_status",hr_status-1));
				}
			
			}
			
			
			
			
		}
		//删除绑定关系
		hotelService.delBind(map);
		//删除用户
		int i = userService.delUser(map);
		
		return i;
	}
	
	
	/**
	 * 一键清空
	 * */
	@ResponseBody
	@RequestMapping("/truncateTable.do")
	public int truncateTable(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		//清空签到
		hxService.truncateSign_in(map);
		//归零房间信息
		hotelService.updateHotelRoomDetails(HUtil.putInMap("hr_status",0));
		//归零床位信息
		hotelService.updateHotelBedDetails(HUtil.putInMap("hb_status",0));
		//清空绑定信息
		hotelService.truncateBind(map);
		//清空用户
		userService.truncateUser(map);
		//清空酒店会议绑定
		hotelService.truncateBindHotelFair();
		
		
		return 1;
	}
	/**
	 * 查询所有会议及指定用户的会议座次
	 * */
	
}